package org.lmj.dos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.AddressEntity;
import org.lmj.dos.mapper.AddressMapper;
import org.lmj.dos.web.resultdo.OrdersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/address")
@RestController
public class AddressController {

    @Autowired
    AddressMapper addressMapper;
    //@GetMapping("/order/confirm")
    //结算订单
    @GetMapping("/get")
    public ServerResponse getAddress(String userId) throws IOException {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        ArrayList<AddressEntity> addressEntities = new ArrayList<>();
        QueryWrapper<AddressEntity> wrapper = new QueryWrapper<>();
        wrapper.in("user_id",userId);
        List<AddressEntity> addressEntity1 = addressMapper.selectList(wrapper);
       // addressEntities.add(addressEntity1);
        stringObjectHashMap.put("list",addressEntity1);
        return ServerResponse.success(stringObjectHashMap);
        //https://www.jianshu.com/p/7d7e5e4e8ae3  product-45-productDetail.json
//        ClassPathResource classPathResource = new ClassPathResource("pay-address-add-get.json");
//        InputStream inputStream =classPathResource.getInputStream();
//        ObjectMapper objectMapper = new ObjectMapper();
//        OrdersVo.OrderConfirmVo getOrders = objectMapper.readValue(inputStream, OrdersVo.OrderConfirmVo.class);
//        return ServerResponse.success(getOrders);
    }

    @PostMapping("/add")
    public ServerResponse addAddress(@RequestBody AddressEntity addressEntity) throws IOException {
        //https://www.jianshu.com/p/7d7e5e4e8ae3  product-45-productDetail.json
        int insert = addressMapper.insert(addressEntity);

//        ClassPathResource classPathResource = new ClassPathResource("pay-address-add-get.json");
//        InputStream inputStream =classPathResource.getInputStream();
//        ObjectMapper objectMapper = new ObjectMapper();
//        OrdersVo.OrderConfirmVo getOrders = objectMapper.readValue(inputStream, OrdersVo.OrderConfirmVo.class);
        Map<String,Object>result=new HashMap<>();
        result.put("addressId",addressEntity.getId());
        return ServerResponse.success(0,"新建地址成功",result);
    }
    @PutMapping("/update")
    public ServerResponse update() throws IOException {
        //https://www.jianshu.com/p/7d7e5e4e8ae3  product-45-productDetail.json
        ClassPathResource classPathResource = new ClassPathResource("pay-address-add-get.json");
        InputStream inputStream =classPathResource.getInputStream();
        ObjectMapper objectMapper = new ObjectMapper();
        OrdersVo.OrderConfirmVo getOrders = objectMapper.readValue(inputStream, OrdersVo.OrderConfirmVo.class);
        return ServerResponse.success(getOrders);
    }

    @DeleteMapping("/delete/{addressId}")
    public ServerResponse delete(@PathVariable("addressId")Integer addressId ) throws IOException {

        return ServerResponse.success(addressMapper.deleteById(addressId));
        //https://www.jianshu.com/p/7d7e5e4e8ae3  product-45-productDetail.json
//        ClassPathResource classPathResource = new ClassPathResource("pay-address-add-get.json");
//        InputStream inputStream =classPathResource.getInputStream();
//        ObjectMapper objectMapper = new ObjectMapper();
//        OrdersVo.OrderConfirmVo getOrders = objectMapper.readValue(inputStream, OrdersVo.OrderConfirmVo.class);
//        Map<String,Object>result=new HashMap<>();
//        result.put("shippingId",5798);
//        return ServerResponse.success(0,"新建地址成功",result);
    }
}
